Ruby On Rails Interview Questions and Answers
Ques 31. What is the purpose of Layouts in Ruby on Rails?
Layouts are partial ruby/html files that are used to render the content pages.
There are placed in the folder: app/views/layouts
Items that you would typically put in this folder are things like headers/footers, navigation elements, etc.
Here’s a sample layout file: /app/views/layout/application.html.erb
<html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Learning System | <%= @page_title || 'Admin Area' %></title> <meta name="author" content="Anil Punjabi"> <%= stylesheet_link_tag('public', 'admin', :media => 'all') %> <%= javascript_include_tag('application') %> </head> <body> <div id="header"> <h1>Learning System</h1> </div> <div id="main"> <% if !flash[:notice].blank? %> <div class="notice"> <%= flash[:notice] %> </div> <% end %> <div id="content"> <%= yield %> </div> </div> <div id="footer"> <p id="copyright">© / Anil Punjabi</p> </div> </body> </html>
Ques 32. IS IT POSSIBLE TO EMBED PARTIAL VIEWS INSIDE LAYOUTS? HOW?
That is the purpose of layouts. You embed partial views inside the file /app/views/layout/application.html.erb and then whenever you render any page this layout is merged with it.
Ques 33. What is Rake in Ruby on Rails?
Rake is a popular ruby gem that makes the job of running tasks simpler.
Rake is most often used for DB tasks, but it can be used for m
The common DB commands are:
rake db:migrate rake db:reset
You can use cron to schedule rake tasks.
Sometimes you would create a dataloader.rake file and put it in the lib/tasks folder so that it can be used to populate the database on startup.
Ques 34. What is Capistrano?
Capistrano is a popular deployment tool — it allows developers to push code from their desktop to the servers.
Ques 35. What is Eager Loading in Ruby on Rails?
Eager loading is a great optimization strategy to reduce the number of queries that are made against the DB.
Say you are finding 10 employees and then you are looking for their post codes. Then your query would appear something like this:
clients = Client.limit(10) clients.each do |client| puts client.address.postcode end
This may seem fine at first look but really this implementation leaves much to be desired. It makes 11 DB calls just to get the results.
Now you can optimize this query by making a slight change in the request like this:
clients = Client.includes(:address).limit(10) clients.each do |client| puts client.address.postcode end
This new request makes two SQL calls like this:
SELECT * FROM clients LIMIT 10 SELECT addresses.* FROM addresses WHERE (addresses.client_id IN (1,2,3,4,5,6,7,8,9,10))
Most helpful rated by users:
- What is Ruby On Rails?
- Why Ruby on Rails?
- Explain how (almost) everything is an object in Ruby.
- What are Gems and which are some of your favorites?
- How would you declare and use a constructor in Ruby?